<template>
  <div>
    <van-nav-bar
      title="账号登录"
      left-text="返回"
      left-arrow
      @click-left="$router.back()"
      style="backgroundcolor: #c8bdcb; color: #ffffff"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        placeholder="请输入账号"
        style="height: 60px; line-height: 60px; font-size: 16px"
        :rules="userRules"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        placeholder="请输入密码"
        style="height: 60px; line-height: 60px; font-size: 16px"
        :rules="pwdRules"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>
<script>
import { loginApi } from "@/api";
import { userRules, pwdRules } from "./rules";

export default {
  name: "",
  data() {
    return {
      username: "",
      password: "",
      userRules,
      pwdRules,
    };
  },
  components: {},
  computed: {},
  methods: {
    async onSubmit(values) {
      try {
        // console.log(values);
        const res = await loginApi(values);
        console.log(res);

        const status = res.data.status;
        const msg = res.data.description;

        if (status === 200) {
          //存token
          const token = res.data.body;
          this.$store.commit("SET_TOKEN", token);
          //跳转我的
          this.$router.back();
          this.$notify({ type: "success", message: msg });
        } else if (status === 400) {
          this.$notify({ type: "danger", message: msg });
        }
      } catch (error) {
        console.log(error);
      }
    },
  },
};
</script>
<style lang="" scoped></style>
